<template>
  <div class="app-container">
    <AddBtn :handleClick="handleCreate" :text="text" v-if="deviceType === 2 || deviceType == 3"/>
    <el-table :data="distributionList" border style="width: 100%">
      <el-table-column align="center" label="ID" prop="id" width="95" type="index"></el-table-column>
      <el-table-column prop="name" label="名称" width="250"></el-table-column>
      <el-table-column prop="isPublic" label="类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isPublic=== 1" type="primary">公有</el-tag>
          <el-tag v-else type="warning">私有</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="300" align="center" v-if="deviceType === 2 || deviceType == 3">
        <template slot-scope="scope">
          <el-button size="small" type="warning" @click="handleUpdate(scope.$index, scope.row)">
            <i class="el-icon-edit"></i>
          </el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDel(scope.$index, scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form label-width="120px" :model="Form" :rules="FormRules" ref="dataForm">
        <el-form-item label="模板名称" prop="name" width="100%">
          <el-input v-model="Form.name"></el-input>
        </el-form-item>
        <el-form-item label="模板类型" prop="isPublic">
          <el-select v-model="Form.isPublic" class="filter-item" placeholder="请选择类型">
            <el-option v-for="item in TypeOptions" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="dialogConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getDistributionList,
  createDistribution,
  updateDistribution,
  deleteDistribution
} from "@/api/distribution";
import AddBtn from "@/views/layout/components/AddBtn";
import { mapGetters } from "vuex";
export default {
  name: "distributionTempDetail",
  props: {
    deviceType: {
      type: Number,
      default: 1
    }
  },
  components: { AddBtn },
  data() {
    return {
      text:"布点方式",
      distributionList: [],
      listLoading: false,
      dialogStatus: "",
      textMap: {
        createIncubator: "新增保温箱布点方式",
        createCar: "新增冷藏车布点方式",
        createWarehouse: "新增库房布点方式",
        updateIncubator: "编辑保温箱布点方式",
        updateCar: "编辑冷藏车布点方式",
        updateWarehouse: "编辑库房布点方式"
      },

      TypeOptions: [{ value: 1, label: "公有" }, { value: 0, label: "私有" }],
      dialogFormVisible: false,
      Form: {
        name: "",
        isPublic: null
      },
      FormRules: {
        name: [{ required: true, message: "请输入布点方式名称", trigger: "blur" }],
        isPublic: [{ required: true, message: "请选择类型", trigger: "change" }],
      }
    };
  },
  created() {
    this.getList();
  },
  computed: {
    ...mapGetters(["customerid"])
  },
  methods: {
    //获取列表
    getList() {
      this.listLoading = true;
      getDistributionList({
        customerId: this.customerid,
        deviceType: this.deviceType
      })
        .then(response => {
          this.distributionList = response.data;
          this.listLoading = false;
        })
        .catch(e => {});
    },
    handleCreate() {
      if (this.deviceType === 1) {
        this.dialogStatus = "createIncubator";
      } else if (this.deviceType === 2) {
        this.dialogStatus = "createCar";
      } else {
        this.dialogStatus = "createWarehouse";
      }
      this.Form = {
        name: "",
        isPublic: null
      };
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate();
      });
    },

    dialogConfirm() {
      if ( this.dialogStatus === "createIncubator" || this.dialogStatus === "createCar" ||  this.dialogStatus === "createWarehouse" ) {
        this.DistributionCreate();
      } else {
        this.DistributionUpdate();
      }
    },

    //新增布点方式
    DistributionCreate() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消"
          }).then(() => {
            let tempData = Object.assign( {}, this.Form, { customerId: this.customerid },  { deviceType: this.deviceType } );
            createDistribution(tempData).then(res => {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: "成功",
                message: "新增布点方式成功",
                type: "success",
                duration: 2000
              });
            });
          });
        }
      });
    },

    handleUpdate(index, row){
      this.Form = Object.assign(this.Form, row);
      if (this.deviceType === 1) {
        this.dialogStatus = "updateIncubator";
      } else if (this.deviceType === 2) {
         this.$router.push({
          path:'/validate/car/distributionTempEdit/'+ row.id
        })
       // this.dialogStatus = "updateCar";
      } else {
        this.dialogStatus = "updateWarehouse";
      }
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate();
      });

    },

    //编辑布点方式
    DistributionUpdate() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          const tempData = Object.assign({}, this.Form);
          updateDistribution(tempData).then(() => {
            this.getList();
            this.dialogFormVisible = false;
            this.$notify({
              title: "成功",
              message: "更新布点方式成功",
              type: "success",
              duration: 2000
            });
          });
        }
      });
    },

    //删除布点方式
    handleDel(index, row) {
      this.$confirm("确认删除该布点方式吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.listLoading = true;
        deleteDistribution({ id: row.id }).then(() => {
          this.listLoading = false;
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 2000
          });
          this.getList();
        });
      }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    }


  }
};
</script>


